




IQ * parseIQ(XmlPullParser *parser) {
	Privacy * privacy = new Privacy();
	/* privacy.addExtension(PacketParserUtils.parsePacketExtension(parser
	   .getName(), parser.getNamespace(), parser)); */
	privacy->addExtension(new DefaultPacketExtension(parser->getName(), parser->getNamespace()));
	bool done = false;
	while (!done) {
		int eventType = parser->next();
		if (eventType == XmlPullParser::START_TAG) {
			if (parser->getName() == "active") {
				std::string activeName = parser->getAttributeValue("", "name");
				if (activeName.empty()) {
					privacy->setDeclineActiveList(true);
				} else {
					privacy->setActiveName(activeName);
				}
			} else if (parser->getName() == "default") {
				std::string defaultName = parser->getAttributeValue("", "name");
				if (defaultName.empty()) {
					privacy->setDeclineDefaultList(true);
				} else {
					privacy->setDefaultName(defaultName);
				}
			} else if (parser->getName() == "list") {
				parseList(parser, privacy);
			}
		} else if (eventType == XmlPullParser::END_TAG) {
			if (parser->getName() == "query") {
				done = true;
			}
		}
	}

	return privacy;
}


void parseList(XmlPullParser * parser, Privacy * privacy) {
    bool done = false;
	std::string listName = parser->getAttributeValue("", "name");
	std::list<PrivacyItem*> items; 
	while (!done) {
		int eventType = parser->next();
		if (eventType == XmlPullParser::START_TAG) {
			if (parser->getName() == "item") {
				items.add(parseItem(parser));
			}
		}
		else if (eventType == XmlPullParser::END_TAG) {
			if (parser->getName() == "list") {
				done = true;
			}
		}
	}

	privacy->setPrivacyList(listName, items);
}


PrivacyItem * parseItem(XmlPullParser * parser) {
	bool done = false;
	// Retrieves the required attributes
	std::string actionValue = parser->getAttributeValue("", "action");
	std::string orderValue = parser->getAttributeValue("", "order");
	std::string type = parser->getAttributeValue("", "type");

	/* 
	 * According the action value it sets the allow status. The fall-through action is assumed 
	 * to be "allow"
	 */
	std::transform(actionValue.begin(), actionValue.end(), actionValue.begin(), ::tolower);
	bool allow = true;
	if (actionValue == "allow") {
		allow = true;
	} else if (actionValue == "deny") {
		allow = false;
	}
	// Set the order number
	int order = atoi(orderValue.c_str());

	// Create the privacy item
	PrivacyItem * item = new PrivacyItem(type, allow, order);
	item->setValue(parser->getAttributeValue("", "value"));

	while (!done) {
		int eventType = parser->next();
		if (eventType == XmlPullParser::START_TAG) {
			if (parser->getName() == "iq") {
				item->setFilterIQ(true);
			}
			if (parser->getName() == "message") {
				item->setFilterMessage(true);
			}
			if (parser->getName() == "presence-in") {
				item->setFilterPresence_in(true);
			}
			if (parser->getName() == "presence-out") {
				item->setFilterPresence_out(true);
			}
		} else if (eventType == XmlPullParser::END_TAG) {
			if (parser->getName() =="item") {
				done = true;
			}
		}
	}
	return item;
}



